#!/usr/bin/ksh

run ()
{
   testName=${1}_${MACHINE}_$2.log
   messageCounter=$3
   clientLoad=$4

   mkdir -p $testName
   
   typeset -i n=0
   typeset -i maxClient=0
   typeset -i port=2000

   > $testName/server.load
   
   printf "Prueba %s " $testName
   while [ $n -lt 10 ]
   do
      printf "."
      $OUTPUT/server_${1} -a $IP -p $port -limit 60 -n $messageCounter -d 25 -trace notice -maxthr $MAXTHREAD > /dev/null &
   
      sleep 1
      $MYPATH/measure_load server_$1 >> $testName/server.load &
      maxClient=$2-1
      while let "maxClient > 0"
      do 
         $OUTPUT//client_${1}  -a $IP -p $port -n $clientLoad -trace notice >> /dev/null &
         maxClient=$maxClient-1;
      done
      $OUTPUT//client_${1} -a $IP -p $port -n $clientLoad -trace notice >> /dev/null 
   
      cat -s server.*.trace.old > $testName/server.$n.trace
      cat -s server.*.trace >>  $testName/server.$n.trace
               
      cat client.*.trace > $testName/client.$n.trace
      
      rm -f *.trace *.old

      ps | while read a
      do
         process=`echo $a | cut -f 4 -d ' '`
         if [ "$process" = "server_$1" ]
         then
            pid=`echo $a | cut -f 1 -d ' '`
            kill -9 $pid
            break;
         fi
      done
 
      n=$n+1
      port=$port+1
   done
   echo
}

constant_client ()
{
   P=`pwd`
   mkdir constant_client
   cd constant_client
   pwd

   run mt 20 1000 24
   run st 20 1000 24

   run mt 16 1000 24
   run st 16 1000 24

   run mt 10 1000 24
   run st 10 1000 24

   run mt 8 1000 24
   run st 8 1000 24

   run mt 4 1000 24
   run st 4 1000 24

   run mt 2 1000 24
   run st 2 1000 24

   run mt 1 1000 24
   run st 1 1000 24
   cd $P
}

constant_load ()
{
   P=`pwd`
   mkdir constant_load
   cd constant_load
   pwd

   run mt 1 1000 320
   run st 1 1000 320
   
   run mt 2 1000 160
   run st 2 1000 160
   
   run mt 4 1000 80
   run st 4 1000 80
   
   run mt 8 1000 40
   run st 8 1000 40
   
   run mt 10 1000 32
   run st 10 1000 32

   run mt 16 1000 20
   run st 16 1000 20

   run mt 20 1000 16
   run st 20 1000 16

   cd $P
}

MACHINE=`hostname`
MYPATH=`pwd`
OUTPUT=/export/temporal/comparative_mt_vs_st/`date -u +%Y%M%d.%H%M%S.output`
MYPATH=`pwd`

IP=`cat hosts/$MACHINE` 
if [ "$IP" = "" ]
then
   echo IP de maquina $MACHINE no registrada en $MYPATH/hosts
   exit 1
fi

mkdir -p $OUTPUT
if [ $? -lt 0 ]
then
   echo No se pudo crear $OUTPUT
   echo No se pudo crear $OUTPUT >> /tmp/run_report.log
   exit 1
fi

SO=`uname`
if [ "$SO" = "SunOS" ]
then
   ln -s $MYPATH/../_solaris8_forte6_debug_st_none_none.O/server $OUTPUT/server_st 
   ln -s $MYPATH/../_solaris8_forte6_debug_st_none_none.O/client $OUTPUT/client_st 
   ln -s $MYPATH/../_solaris8_forte6_debug_mt_none_none.O/server $OUTPUT/server_mt
   ln -s $MYPATH/../_solaris8_forte6_debug_mt_none_none.O/client $OUTPUT/client_mt
else 
   ln -s $MYPATH/../_linux2.4.21_gcc3.3.3_debug_st_none_none.O/server $OUTPUT/server_st 
   ln -s $MYPATH/../_linux2.4.21_gcc3.3.3_debug_st_none_none.O/client $OUTPUT/client_st 
   ln -s $MYPATH/../_linux2.4.21_gcc3.3.3_debug_mt_none_none.O/server $OUTPUT/server_mt 
   ln -s $MYPATH/../_linux2.4.21_gcc3.3.3_debug_mt_none_none.O/client $OUTPUT/client_mt 
fi

if [ "$1" = "" ]
then
   MAXTHREAD=8
else
   MAXTHREAD=$1
fi

cd $OUTPUT 

constant_load 
constant_client 

cd $MYPATH


